#!/usr/bin/env bash

setup_android_studio()
{
    # Install the android-studio package.
    brew cask install android-studio
}

setup_chromium()
{
    # Install the eloston-chromium package.
    brew cask install eloston-chromium
}

setup_docker()
{
    # Install the docker package.
    brew cask install docker
}

setup_figma()
{
    # Install the figma package.
    brew cask install figma
}

setup_flutter()
{
    # Install the flutter package.
    brew tap flschweiger/flutter
    brew cask install flutter

    # Accept the android licenses.
    (for run in {1..1000}; do echo y; done) >> yep.txt
    cat ./yep.txt | flutter doctor --android-licenses
    rm ./yep.txt

    # TODO: Install the flutter plugin into android-studio.
    # TODO: Install the dart plugin into android-studio.
}

setup_git()
{
    # Install the git package.
    brew install git

    # Edit the settings.
    git config --global credential.helper 'cache --timeout=21600'
    git config --global user.email 'anonymous@example.com'
    git config --global user.name 'anonymous'
}

setup_homebrew()
{
    # Install the homebrew package.
    echo | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

    # Update and clean up homebrew.
    brew update && brew cleanup

    # Install the coreutils package.
    brew install coreutils
}

setup_iina()
{
    # Install the iina package.
    brew cask install iina

    # Edit the settings.
}

setup_iterm2()
{
    # Install the iterm2 package.
    brew cask install iterm2
}

setup_jdownloader()
{
    # Install the jdownloader package.
    brew cask install jdownloader

    # TODO: Edit the settings.
    # settings="/Applications/JDownloader 2.0/cfg/org.jdownloader.settings.GraphicalUserInterfaceSettings.json"
    # open -a JDownloader2
    # sleep 5 && while [ ! -f "${settings}" ]; do sleep 2; done
    # pkill -f 'JDownloader2' && sleep 5
    # open -a JDownloader2
    # sleep 5 && pkill -f 'JDownloader2' && sleep 5
    # sed -i "s/\"bannerenabled\".*/\"bannerenabled\" : false,/g" "${settings}"
    # sed -i "s/\"donatebuttonstate\".*/\"donatebuttonstate\" : \"USER_HIDDEN\",/g" "${settings}"
    # sed -i "s/\"myjdownloaderviewvisible\".*/\"myjdownloaderviewvisible\" : false,/g" "${settings}"
    # sed -i "s/\"speedmetervisible\".*/\"speedmetervisible\" : false,/g" "${settings}"
}

setup_keka()
{
    # Install the keka package.
    brew cask install keka kekadefaultapp
}

setup_lyricsx()
{
    # Install the lyricsx package.
    brew cask install lyricsx
}

setup_macos()
{
    # Disable the Gate Keeper.
    sudo spctl --master-disable
    sudo defaults write /var/db/SystemPolicy-prefs.plist enabled -string no
    defaults write com.apple.LaunchServices LSQuarantine -bool false

    # Show all filename extensions in Finder by default.
    defaults write NSGlobalDomain AppleShowAllExtensions -bool true

    # Enable subpixel font rendering on non-apple monitors.
    defaults write NSGlobalDomain AppleFontSmoothing -int 2

    # Set the icon size of dock items to 48 pixels.
    defaults write com.apple.dock tilesize -int 48

    # Enable dock to auto-hide.
    defaults write com.apple.dock autohide -bool true

    # Avoid the creation of ".DS_Store" files on network volumes.
    defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
}

setup_mockoon()
{
    # Install the mockoon package.
    brew cask install mockoon
}

setup_mpv()
{
    # Install the mpv package.
    brew cask install mpv

    # Edit the settings.
    settings="${HOME}/.config/mpv/mpv.conf"
    mkdir -p "$(dirname "${settings}")" && cat /dev/null > "${settings}"
    echo 'profile=gpu-hq' | tee -a "${settings}"
    echo 'hwdec=auto' | tee -a "${settings}"
    echo 'keep-open=yes' | tee -a "${settings}"
    echo 'ytdl-format="bestvideo[height<=?1080][vcodec!=vp9]+bestaudio/best"' | tee -a "${settings}"
}

setup_node()
{
    # Install the node and yarn packages.
    brew install node yarn
}

setup_postman()
{
    # Install the postman package.
    brew cask install postman
}

setup_tiles()
{
    # Install the tiles package.
    brew cask install tiles
}

setup_transmission()
{
    # Install the transmission package.
    brew cask install transmission

    # Edit the settings.
    defaults write org.m0k.transmission WarningDonate -bool false
    defaults write org.m0k.transmission RatioCheck -bool true
    defaults write org.m0k.transmission RatioLimit -int 0
}

setup_visual_studio_code()
{
    # Install the visual-studio-code package.
    brew cask install visual-studio-code

    # Install the font-cascadia package.
    brew tap homebrew/cask-fonts
    brew cask install font-cascadia

    # Install some extensions.
    code --install-extension dart-code.flutter
    code --install-extension github.github-vscode-theme

    # Edit the settings.
    settings="${HOME}/Library/Application Support/Code/User/settings.json"
    mkdir -p $(dirname "${settings}") && cat /dev/null | tee "${settings}"
    echo '{' | tee -a "${settings}"
    echo '    "editor.fontFamily": "Cascadia Code, monospace",' | tee -a "${settings}"
    echo '    "editor.fontSize": 14,' | tee -a "${settings}"
    echo '    "editor.lineHeight": 38,' | tee -a "${settings}"
    echo '    "telemetry.enableTelemetry": false,' | tee -a "${settings}"
    echo '    "telemetry.enableCrashReporter": false,' | tee -a "${settings}"
    echo '    "workbench.colorTheme": "GitHub Dark"' | tee -a "${settings}"
    echo '}' | tee -a "${settings}"
}

setup_youtube_dl()
{
    # Install the youtube-dl package.
    brew install youtube-dl
}

setup_xcode()
{
    # TODO: Install the xcode package.
    # Needs to be signed into the Mac App Store.
    # brew install mas && mas install 497799835
    # https://github.com/notpeter/apple-installer-checksums/blob/master/xcode.md
    sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
    sudo xcodebuild -runFirstLaunch

    # Install the cocoapods package.
    sudo gem install cocoapods
}

main()
{
    echo 'Installing and configuring homebrew...'
    setup_homebrew > /dev/null 2>&1

    echo 'Installing and configuring git...'
    setup_git > /dev/null 2>&1

    ###

    echo 'Installing and configuring android-studio...'
    setup_android_studio > /dev/null 2>&1

    echo 'Installing and configuring visual-studio-code...'
    setup_visual_studio_code > /dev/null 2>&1

    echo 'Installing and configuring xcode...'
    setup_xcode > /dev/null 2>&1

    ###

    echo 'Installing and configuring iterm2...'
    setup_iterm2 > /dev/null 2>&1

    echo 'Installing and configuring flutter...'
    setup_flutter > /dev/null 2>&1

    echo 'Installing and configuring mockoon...'
    setup_mockoon > /dev/null 2>&1

    echo 'Installing and configuring node...'
    setup_node > /dev/null 2>&1

    echo 'Installing and configuring postman...'
    setup_postman > /dev/null 2>&1

    ###

    echo 'Installing and configuring figma...'
    setup_figma > /dev/null 2>&1

    ###

    echo 'Installing and configuring chromium...'
    setup_chromium > /dev/null 2>&1

    echo 'Installing and configuring jdownloader...'
    setup_jdownloader > /dev/null 2>&1

    echo 'Installing and configuring transmission...'
    setup_transmission > /dev/null 2>&1

    ###

    echo 'Installing and configuring iina...'
    setup_iina > /dev/null 2>&1

    echo 'Installing and configuring lyricsx...'
    setup_lyricsx > /dev/null 2>&1

    echo 'Installing and configuring mpv...'
    setup_mpv > /dev/null 2>&1

    echo 'Installing and configuring youtube-dl...'
    setup_youtube_dl > /dev/null 2>&1

    ##

    echo 'Installing and configuring keka...'
    setup_keka > /dev/null 2>&1

    echo 'Installing and configuring tiles...'
    setup_tiles > /dev/null 2>&1
}
